package com.googlecode.agscrum.model.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;

@Entity
@SequenceGenerator(name = "permission_sequence", sequenceName = "permission_sequence")
public class PermissionProject {
	@Id
	@GeneratedValue(generator = "permission_sequence", strategy = GenerationType.SEQUENCE)
	@Column(name = "id_permission")
	private Long id;
	@ManyToOne
	@JoinColumn(name = "id_user")
	private User user;
	@ManyToOne
	private Product product;
	@ManyToMany
	@JoinTable(name = "PERMISSION_ROLE", joinColumns = @JoinColumn(name = "id_permission", referencedColumnName = "id_permission"), inverseJoinColumns = @JoinColumn(name = "id_role", referencedColumnName = "id_role"))
	private Set<Role> roles;

	public void addRole(Role role) {
		if (roles == null) {
			roles = new HashSet<Role>();
		}
		roles.add(role);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public void removeRole(Role role) {
		if (roles != null && roles.contains(role)) {
			roles.remove(role);
		}
	}

	public int getTotalRoles() {
		if (roles == null || roles.size() == 0)
			return 0;
		else
			return roles.size();
	}

}
